#include "leetcode_util.h"

int helper(vector<int>& A, vector<int>& B, string swapbit, int len_ab, int start) {
    if (start == len_ab-1) return 0;
    for (int i = start; i < len_ab-1; i++) {
        if (swapbit[i] == '1') {
            if (A[i] < B[i+1] && B[i] < A[i+1]) {
                continue;
            }
            swapbit[i+1] = '1';
            return 1 + helper(A, B, swapbit, len_ab, i+1);
        } else {
            if (A[i] < A[i+1] && B[i] < B[i+1]) {
                continue;
            }
            // x,x,x,1,7,11,12,13
            // x,x,x,4,3,21,22,23
            // case1: 1 <-> 4
            swapbit[i] = '1';
            int res1 = 1 + helper(A, B, swapbit, len_ab, i+1);
            swapbit[i+1] = '1';
            int res2 = 1 + helper(A, B, swapbit, len_ab, i+1);
            return res1 <= res2 ? res1 : res2;
        }
    }
    return 0;
}

int numberOfArithmeticSlices(vector<int>& A) {
    // code

    int res = 0, n = A.size();
    vector<int> dp(n, 0);
    for (int i = 2; i < n; ++i) {
        if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
            dp[i] = dp[i - 1] + 1;
        }
        res += dp[i];
    }
    return res;
}

void showVector(vector<int>& A) {
    vector<int>::iterator itr = A.begin();
    for(; itr != A.end();) {
        printf("%d", *itr);
        if (++itr != A.end()) {
            printf(" ");
        } else {
            printf("\n");
        }
    }
}

void testVector(int argc, char *argv[]) {
    string s;
    for (int i = 1; i < argc; i++) {
        s = string(argv[i]);
        vector<int> v = stringToVector(s);
        // test function

        int res = numberOfArithmeticSlices(v);
        cout << res << endl;
    }
}

void test_minSwap(int argc, char *argv[]) {
    string s1 = string(argv[1]);
    string s2 = string(argv[2]);
    vector<int> v1 = stringToVector(s1);
    vector<int> v2 = stringToVector(s2);
    //cout << solution(v1, v2) << endl;
}

int main(int argc, char *argv[]) {
    testVector(argc, argv);
}

